package com.example.demo.service.impl;

import com.example.demo.mqtt.AppProviderHelper;
import com.zkkz.boot.message.pojo.MessageStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author：Chan
 * @Package：com.zkkz.boot.mqtt.service
 * @Project：wisdom-cloud-platform
 * @name：MqttServiceImpl
 * @Date：2023/9/13 18:23
 * @Describe：MqttServiceImpl
 */
@Service
public class AppServiceImpl {
    @Resource
    private AppProviderHelper appProviderHelper;

    public boolean sendMessage(String topic, String message) {
        return appProviderHelper.publish(topic, 1, false, message);
    }
    public boolean sendMessageOrigin(String topic, String message) {
        return appProviderHelper.publish(topic, 1, false, message);
    }

    public List<MessageStatus> sendMessages(String topic, HashMap<String, String> message) {
        List<MessageStatus> messageStatuses = message.entrySet().stream().map(x -> {
            boolean published = appProviderHelper.publish(topic, 1, false, x.getValue());
            return MessageStatus.builder().messageId(x.getKey()).published(published).build();
        }).collect(Collectors.toList());
        return messageStatuses;
    }

}
